package com.lancoo.cvas.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lancoo.cvas.entity.*;
import com.lancoo.cvas.mapper.ClassInfoMapper;
import com.lancoo.cvas.mapper.RoomMapper;
import com.lancoo.cvas.mapper.TeachingClassMapper;
import com.lancoo.cvas.pojo.common.CollComparatorUtil;
import com.lancoo.cvas.pojo.common.Deleted;
import com.lancoo.cvas.pojo.common.ScheduleState;
import com.lancoo.cvas.pojo.constant.GlobalConstant;
import com.lancoo.cvas.pojo.response.digitization.BuildingInfo;
import com.lancoo.cvas.pojo.vo.*;
import com.lancoo.cvas.service.*;
import com.lancoo.cvas.service.RoomService;
import com.lancoo.cvas.service.TaskInfoService;
import com.lancoo.cvas.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

@Slf4j
@Service("roomService")
public class RoomServiceImpl extends ServiceImpl<RoomMapper, Room> implements RoomService {

    @Resource
    private CollComparatorUtil<Room> collComparatorUtil;

    @Resource
    private RoomMapper roomMapper;

    @Resource
    private TaskInfoService taskInfoService;

    @Resource
    private ClassInfoMapper classInfoMapper;

    @Resource
    private PlanTeachingCourseSegmentService planTeachingCourseSegmentService;

    @Resource
    private PlanTeachingService planTeachingService;

    @Resource
    private CourseService courseService;

    @Resource
    private SegmentService segmentService;
    @Resource
    private TeachingClassMapper teachingClassMapper;
    @Resource
    private ScheduleService scheduleService;

    @Resource
    private ClassRoomService classRoomService;
    @Resource
    private ClassInfoService classInfoService;
    @Resource
    private TeachingClassWeekService teachingClassWeekService;
    @Resource
    private BasicRuleService basicRuleService;


    @Override
    public void syncRooms(Integer taskId, String schoolId, String term, List<BuildingInfo> buildingInfos) {
        List<Room> rooms = new ArrayList<>();
        if (CollUtil.isEmpty(buildingInfos)) {
            return;
        }

        for (BuildingInfo buildingInfo : buildingInfos) {
            for (BuildingInfo.BuildingFloorsDTO baseFloor : buildingInfo.getBuildingFloors()) {
                for (BuildingInfo.BuildingFloorsDTO.FloorRoomsDTO floorRoom : baseFloor.getFloorRooms()) {
                    BuildingInfo.BuildingFloorsDTO.FloorRoomsDTO.RoomInfoDTO baseRoom = floorRoom.getRoomInfo();
                    //只同步可以排课的教室类型
                    if (GlobalConstant.roomTypeMap.containsKey(baseRoom.getFunctionType())) {
                        rooms.add(new Room()
                                .setTaskId(taskId)
                                .setBaseRoomId(baseRoom.getRoomId())
                                .setRoomName(baseRoom.getRoomName())
                                .setBuildingFloor(baseFloor.getBuildFloor())
                                .setBuildingId(buildingInfo.getBuildingInfo().getBuildingId())
                                .setBuildingName(buildingInfo.getBuildingInfo().getBuildingName())
                                .setRoomType(baseRoom.getFunctionType())
                                .setRoomNum((baseRoom.getSeatCount() == null || baseRoom.getSeatCount() == 0) ? 80 : baseRoom.getSeatCount())
                                .setCampusId(schoolId + baseRoom.getCampus())
                                .setCampusName(baseRoom.getCampus())
                                .setSchoolId(schoolId)
                                .setTerm(term)
                                .setIsEnable(1)
                                .setDeleted(0)
                        );
                    }
                }
            }
        }

        List<Integer> baseRoomIds = roomMapper.selectRoomIds(schoolId, term);
        if (CollUtil.isNotEmpty(baseRoomIds)) {
            rooms = rooms.stream().filter(p -> !baseRoomIds.contains(p.getBaseRoomId())).collect(Collectors.toList());

            //合并新老教室
//            List<Room> addRoom = collComparatorUtil.getAdded(oldRoomList, rooms);
        }

        if (CollUtil.isNotEmpty(rooms)) {
            roomMapper.addBatch(rooms);
        }

    }

    @Override
    public List<Campus> getCampusList(Integer taskId) {
        TaskInfo taskInfo = taskInfoService.getById(taskId);
        if (taskInfo == null) {
            return new ArrayList<>();
        }

        //同过教室获取校区
        return roomMapper.getCampusList(taskInfo.getSchoolId(), taskInfo.getTerm());
    }

    @Override
    public List<Room> getRomList(Integer taskId, String schoolId, Integer roomType, String campusId, String term) {

        //获取教室列表
        return roomMapper.selectList(new LambdaQueryWrapper<Room>()
                .eq(StrUtil.isNotBlank(schoolId), Room::getSchoolId, schoolId)
                .eq(ObjUtil.isNotNull(roomType), Room::getRoomType, roomType)
                .eq(StrUtil.isNotBlank(campusId), Room::getCampusId, campusId)
                .eq(StrUtil.isNotBlank(term), Room::getTerm, term)
                .eq(Room::getDeleted, Deleted.NOT.getCode()));
    }


    @Transactional
    @Override
    public String setRoom(Integer taskId, String classId, Long roomId, String schoolId) {


        //设置教室
        List<ClassRoom> list = classRoomService.lambdaQuery().eq(ClassRoom::getTaskId, taskId).eq(ClassRoom::getDeleted, Deleted.NOT.getCode()).list();

        List<Long> collect = list.stream().map(ClassRoom::getRoomId).collect(Collectors.toList());

        if (collect.contains(roomId)) {

            return "该教室已被设置，请重新选择教室";
        }

        ClassInfo classInfo = classInfoMapper.selectOne(new LambdaQueryWrapper<ClassInfo>().eq(ClassInfo::getTaskId, taskId)
                .eq(ClassInfo::getClassId, classId)
                .eq(ClassInfo::getSchoolId, schoolId));

        List<ClassRoom> collect2 = list.stream().filter(p -> p.getClassId().equals(classInfo.getId())).distinct().collect(Collectors.toList());


        if (CollUtil.isNotEmpty(collect2)) {
            List<Long> classroomIds = collect2.stream().map(ClassRoom::getId).distinct().collect(Collectors.toList());
            classRoomService.lambdaUpdate().eq(ClassRoom::getTaskId, taskId).in(ClassRoom::getId, classroomIds).remove();
        }


        classInfoMapper.update(null, new LambdaUpdateWrapper<ClassInfo>()
                .eq(ClassInfo::getTaskId, taskId)
                .eq(ClassInfo::getClassId, classId)
                .eq(ClassInfo::getSchoolId, schoolId)
                .set(ClassInfo::getRoomId, roomId));


        ClassRoom classRoom = new ClassRoom();
        classRoom.setTaskId(taskId);
        classRoom.setClassId(classInfo.getId());
        classRoom.setRoomId(roomId);
        classRoom.setDeleted(Deleted.NOT.getCode());
        classRoomService.save(classRoom);

        return "行政班设置完成";


    }

    @Override
    public List<Course> getCourseList(Integer taskId, String majorId, String gradeId) {

        List<Course> courseList = new ArrayList<>();
        List<Course> courses = courseService.lambdaQuery()
                .eq(Course::getTaskId, taskId)
                .eq(Course::getIsEnable, 0)
                .eq(Course::getDeleted, Deleted.NOT).list();

        List<PlanTeachingCourseSegment> planTeachingCourseSegments = planTeachingCourseSegmentService.lambdaQuery()
                .eq(PlanTeachingCourseSegment::getTaskId, taskId).eq(PlanTeachingCourseSegment::getDeleted, Deleted.NOT).list();
        List<PlanTeaching> planTeachings = planTeachingService.lambdaQuery().eq(PlanTeaching::getTaskId, taskId)
                .eq(PlanTeaching::getDeleted, Deleted.NOT).list();

        List<PlanTeaching> pTeaching = planTeachings.stream()
                .filter(p -> p.getGradeId()
                        .equals(gradeId)
                        && p.getBaseMajorId().equals(majorId))
                .collect(Collectors.toList());

        Set<String> baseCourse = new HashSet<>();
        if (CollUtil.isNotEmpty(pTeaching)) {
            for (PlanTeaching planTeaching : pTeaching) {

                List<PlanTeachingCourseSegment> pTeachingCourseSegments = planTeachingCourseSegments.stream()
                        .filter(p -> p.getBaseTeachingPlanId().equals(planTeaching.getBaseTeachingPlanId())
                                && p.getFlag() == 1).collect(Collectors.toList());


                if (CollUtil.isNotEmpty(pTeachingCourseSegments)) {
                    for (PlanTeachingCourseSegment pTeachingCourseSegment : pTeachingCourseSegments) {

                        if (!baseCourse.contains(pTeachingCourseSegment.getBaseCourseId())) {
                            baseCourse.add(pTeachingCourseSegment.getBaseCourseId());
                        }

                    }
                }
            }
        }


        //判断课程是否被启用

        if (CollUtil.isNotEmpty(baseCourse)) {
            for (String c : baseCourse) {
                List<Course> coursesCount = courses.stream()
                        .filter(p -> p.getBaseCourseId().equals(c))
                        .collect(Collectors.toList());
                courseList.addAll(coursesCount);
            }
        }

        return courseList;

    }

    @Override
    public List<Segment> getSegmentList(Integer taskId, String majorId, String gradeId) {
        List<Segment> segmentList = new ArrayList<>();
        List<Segment> sgements = segmentService.lambdaQuery()
                .eq(Segment::getTaskId, taskId).eq(Segment::getIsEnable, 1)
                .eq(Segment::getDeleted, Deleted.NOT).list();

        List<PlanTeachingCourseSegment> planTeachingCourseSegments = planTeachingCourseSegmentService.lambdaQuery()
                .eq(PlanTeachingCourseSegment::getTaskId, taskId).eq(PlanTeachingCourseSegment::getDeleted, Deleted.NOT).list();
        List<PlanTeaching> planTeachings = planTeachingService.lambdaQuery().eq(PlanTeaching::getTaskId, taskId)
                .eq(PlanTeaching::getDeleted, Deleted.NOT).list();

        List<PlanTeaching> pTeaching = planTeachings.stream()
                .filter(p -> p.getGradeId()
                        .equals(gradeId)
                        && p.getBaseMajorId().equals(majorId))
                .collect(Collectors.toList());

        Set<String> baseSegment = new HashSet<>();
        if (CollUtil.isNotEmpty(pTeaching)) {
            for (PlanTeaching planTeaching : pTeaching) {

                List<PlanTeachingCourseSegment> pTeachingCourseSegments = planTeachingCourseSegments.stream()
                        .filter(p -> p.getBaseTeachingPlanId().equals(planTeaching.getBaseTeachingPlanId())
                                && p.getFlag() == 2).collect(Collectors.toList());


                if (CollUtil.isNotEmpty(pTeachingCourseSegments)) {
                    for (PlanTeachingCourseSegment pTeachingCourseSegment : pTeachingCourseSegments) {

                        if (!baseSegment.contains(pTeachingCourseSegment.getBaseCourseId())) {
                            baseSegment.add(pTeachingCourseSegment.getBaseCourseId());
                        }

                    }
                }
            }
        }


        //判断课程是否被启用

        if (CollUtil.isNotEmpty(baseSegment)) {
            for (String c : baseSegment) {
                List<Segment> segmentCount = sgements.stream()
                        .filter(p -> p.getBaseSegmentId().equals(c))
                        .collect(Collectors.toList());
                segmentList.addAll(segmentCount);
            }
        }

        return segmentList;
    }

    /**
     * @param taskId:
     * @param schoolId:
     * @param term:
     * @Description: 获取教室树形结构
     * @Author: liufeng
     * @Date: 2023/12/6 15:11
     * @return:
     */
    @Override
    public List<RoomVo> getRoomTree(Integer taskId, String schoolId, String term, Integer module) {
        //同一学期只同步一次教室
        List<Room> roomList = this.lambdaQuery().eq(Room::getSchoolId, schoolId)
                .eq(Room::getTerm, term).eq(Room::getDeleted, Deleted.NOT.getCode()).list();

        roomList.forEach(o -> {
            o.setScheduleState(true);
        });

        if (module == 2) {
            Page<TeachingClassCourseDto> page = new Page<>(1, -1);
            List<TeachingClassCourseDto> records = teachingClassMapper.getCourseTeachingClassForArrange(page, taskId, null, null, null, null).getRecords();
            List<TeachingClassCourseDto> unRange = records.stream().filter(o -> o.getState() == ScheduleState.UNARRANGE.getCode()
                    || o.getState() == ScheduleState.CONFLICT.getCode()).collect(Collectors.toList());


            List<Schedule> schedules = scheduleService.lambdaQuery().eq(Schedule::getTaskId, taskId)
                    .eq(Schedule::getDeleted, Deleted.NOT.getCode())
                    .list();
            List<Long> allRoomIds = schedules.stream().map(Schedule::getRoomId).collect(Collectors.toList());
            roomList = roomList.stream().filter(o -> allRoomIds.contains(o.getRoomId())).collect(Collectors.toList());


            if (CollUtil.isNotEmpty(unRange)) {
//                List<Long> teachingClassIds = unRange.stream()
//                        .map(TeachingClassCourseDto::getTeachingClassId)
//                        .distinct().collect(Collectors.toList());
//                HashSet<Long> roomIds = new HashSet<>();
//                        schedules.stream()
//                        .filter(o -> teachingClassIds.contains(o.getTeachingClassId()))
//                        .map(Schedule::getRoomId)
//                        .collect(Collectors.toList());
                List<Long> roomIds = unRange.stream().map(TeachingClassCourseDto::getRoomId).collect(Collectors.toList());

                roomList.forEach(o -> {
                    if (roomIds.contains(o.getRoomId())) {
                        o.setScheduleState(false);
                    }
                });
            }
        }
        List<RoomVo> roomVo = Room.toRoomTree(roomList);
        return roomVo;
    }


    @Override
    public List<Room> getBuildingList(Integer taskId, String schoolId, String term, String campusId, Integer module) {
        if (module == null) {
            Integer unifiedCampus = taskInfoService.getById(taskId).getUnifiedCampus();
            if (1 == unifiedCampus) {
                campusId = null;
            }
        }
        return roomMapper.getBuildingList(schoolId, term, campusId);
    }

    @Override
    public List<Room> getRoomListByBuilding(Integer taskId, String campusId, Integer buildingId, String schoolId, String term) {
        Integer unifiedCampus = taskInfoService.getById(taskId).getUnifiedCampus();
        if (1 == unifiedCampus) {
            campusId = null;
        }
        return roomMapper.getRoomListByBuilding(buildingId, schoolId, term, campusId);
    }

    @Override
    public List<Room> selectRoomList(String schoolId, String term) {
        return roomMapper.selectRoomList(schoolId, term);
    }

    @Override
    public List<Room> getOtherRoomList(Integer taskId, String schoolId, Integer roomType, String campusId, String term) {


        //获取对应教室信息
        List<Room> roomList = roomMapper.selectList(new LambdaQueryWrapper<Room>()
                .eq(StrUtil.isNotBlank(schoolId), Room::getSchoolId, schoolId)
                .eq(ObjUtil.isNotNull(roomType), Room::getRoomType, roomType)
                .eq(StrUtil.isNotBlank(campusId), Room::getCampusId, campusId)
                .eq(StrUtil.isNotBlank(term), Room::getTerm, term)
                .eq(Room::getDeleted, Deleted.NOT.getCode()));
        List<ClassRoom> list = classRoomService.lambdaQuery().eq(ClassRoom::getTaskId, taskId).eq(ClassRoom::getDeleted, Deleted.NOT.getCode()).list();

        List<Long> roomIds = list.stream().map(ClassRoom::getRoomId).distinct().collect(Collectors.toList());

        List<Room> collect = roomList.stream().filter(p -> !roomIds.contains(p.getRoomId())).distinct().collect(Collectors.toList());

        return collect;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result getAutoDistributionsRoom(Integer taskId) {

        TaskInfo taskInfo = taskInfoService.getById(taskId);

        List<TaskInfo> taskInfoList = taskInfoService.lambdaQuery().eq(TaskInfo::getSchoolId, taskInfo.getSchoolId()).eq(TaskInfo::getTerm, taskInfo.getTerm()).list();

        List<Integer> taskIds = taskInfoList.stream().map(TaskInfo::getTaskId).distinct().collect(Collectors.toList());

        List<ClassInfo> classInfoList = classInfoMapper.getClassInfoByTaskIds(taskIds);

        //需要分配的教室
        ClassInfoByClassResponses classInfoByClass = basicRuleService.getClassInfoByClass(taskId, null, null, 1, 1000);

        List<String> classIds = new ArrayList<>();
        if (ObjUtil.isNotNull(classInfoByClass)) {
            List<ClassInfoVo> facultyClassInfoList = classInfoByClass.getFacultyClassInfoList();
            if (CollUtil.isNotEmpty(facultyClassInfoList)) {
                classIds = facultyClassInfoList.stream().map(ClassInfoVo::getClassId).distinct().collect(Collectors.toList());
            }
        }


        //行政班通过校区进行分组
        List<ClassInfo> classInfoList1 = new ArrayList<>();
        Map<String, List<ClassInfo>> classMap = new HashMap<>();
        if (CollUtil.isNotEmpty(classIds)) {
            List<String> finalClassIds = classIds;
            classInfoList1 = classInfoList.stream().filter(p -> p.getTaskId().equals(taskId) && ObjUtil.isEmpty(p.getRoomId())
                    && finalClassIds.contains(p.getClassId())).collect(Collectors.toList());
            classMap = classInfoList1.stream().collect(Collectors.groupingBy(ClassInfo::getCampusId));
        }



        //教室通过校区进行分组
        List<Room> roomList = roomMapper.selectList(new LambdaQueryWrapper<Room>().eq(Room::getSchoolId, taskInfo.getSchoolId())
                .eq(Room::getTerm, taskInfo.getTerm()).eq(Room::getDeleted, Deleted.NOT.getCode()));


        List<Room> collect = roomList.stream().filter(p -> p.getRoomType().equals(21) || p.getRoomType().equals(22) || p.getRoomType().equals(23) || p.getRoomType().equals(24))
                .distinct().collect(Collectors.toList());


        classInfoList.removeAll(classInfoList1);


        //其他学期计划所占教室id
        List<Long> collect1 = classInfoList.stream().filter(p -> ObjUtil.isNotNull(p.getRoomId()))
                .map(ClassInfo::getRoomId).collect(Collectors.toList());

        List<Room> deletedRoomList = collect.stream().filter(p -> collect1.contains(p.getRoomId()))
                .distinct().collect(Collectors.toList());

        if (CollUtil.isNotEmpty(deletedRoomList)) {
            collect.removeAll(deletedRoomList);
        }


        Map<String, List<Room>> roomMap = collect.stream().collect(Collectors.groupingBy(Room::getCampusId));
        AtomicInteger count = new AtomicInteger();

        Set<String> roomCampus = roomMap.keySet();

        Set<String> campus = new HashSet<>();
        classMap.keySet().forEach(o->{

            if(!roomCampus.contains(o)){
                campus.add(o);
            }
        });

        if(CollUtil.isNotEmpty(campus)){
            List<String> campusName = new ArrayList<>();
            for (String campusId : campus) {
                List<ClassInfo> classInfos = classMap.get(campusId);

                if (CollUtil.isNotEmpty(classInfos)) {
                    ClassInfo classInfo = classInfos.get(0);
                    campusName.add(classInfo.getCampusName());
                }
            }

          return   Result.ofFail("校区" + String.join(",", campusName) + "未拥有足够的教室,请手动进行调整");

        }

        //然后对比教室和行政班的数量
        List<String> campusIds = new ArrayList<>();
        classMap.forEach((key, value) -> {
            roomMap.forEach((r, m) -> {
                if (key.equals(r)) {
                    if (m.size() < value.size()) {
                        count.getAndIncrement();
                        campusIds.add(r);
                    }
                }
            });
        });

        //判断校区是否有足量教室
        if (count.intValue() > 0) {
            List<String> campusName = new ArrayList<>();
            for (String campusId : campusIds) {
                List<ClassInfo> classInfoList2 = classMap.get(campusId);
                if (CollUtil.isNotEmpty(classInfoList2)) {
                    ClassInfo classInfo = classInfoList2.get(0);
                    campusName.add(classInfo.getCampusName());
                }
            }
            return Result.ofFail("校区" + String.join(",", campusName) + "未拥有足够的教室,请手动进行调整");
        }


        //给对应校区行政班分配对应教室
        List<ClassInfo> classInfos = new ArrayList<>();

        for (List<ClassInfo> value : classMap.values()) {

            ClassInfo classInfo = value.get(0);
            List<Room> roomList1 = roomMap.get(classInfo.getCampusId());
            if(!CollUtil.isEmpty(roomList1)){
                int roomNum = 0;
                for (ClassInfo classInfo1 : value) {
                    Room room = roomList1.get(roomNum);
                    classInfo1.setRoomId(room.getRoomId());
                    classInfos.add(classInfo1);
                    roomNum++;
                }
            }
        }


        classInfoService.saveOrUpdateBatch(classInfos);

        return Result.ofSuccess("教室分配成功");
    }

    @Override
    public List<RoomDto> getOtherRoomListV2(Integer taskId, String schoolId, Integer roomType, String campusId, String term) {

        TaskInfo taskInfo = taskInfoService.getById(taskId);


        //获取教室信息
        List<RoomDto> roomDtos = roomMapper.getRoomWithClassInfo(taskInfo.getSchoolId(), taskInfo.getTerm(), campusId);


        //获取当前计划和未分配教室的教室信息
        List<RoomDto> collect = roomDtos.stream().filter(p -> ObjUtil.isEmpty(p.getTaskId()) || p.getTaskId().equals(taskId)).collect(Collectors.toList());

        List<RoomDto> collect1 = new ArrayList<>();
        //获取未分配教室
        List<RoomDto> collect2 = collect.stream().filter(p -> ObjUtil.isEmpty(p.getTaskId())).collect(Collectors.toList());
        if (CollUtil.isNotEmpty(collect2)) {
            //通过教室名称进行排序
            List<RoomDto> collect3 = collect2.stream().sorted(Comparator.comparing(RoomDto::getRoomName)).collect(Collectors.toList());
            collect1.addAll(collect3);
        }

        //获取当前计划分配教室
        List<RoomDto> collect3 = collect.stream().filter(p -> ObjUtil.isNotNull(p.getTaskId()) && p.getTaskId().equals(taskId)).collect(Collectors.toList());
        if (CollUtil.isNotEmpty(collect3)) {
            List<RoomDto> collect4 = collect3.stream().sorted(Comparator.comparing(RoomDto::getRoomName)).collect(Collectors.toList());
            collect1.addAll(collect4);
        }

        return collect1;
    }

    @Override
    public List<Room> getSubRoomList(Integer taskId, String subId) {

        //获取拆班教室
        List<TeachingClass> teachingClassList = teachingClassMapper.selectList(new LambdaQueryWrapper<TeachingClass>()
                .eq(TeachingClass::getTaskId, taskId)
                .eq(TeachingClass::getUniqueShiftSign, subId)
                .eq(TeachingClass::getDeleted, Deleted.NOT.getCode()));
        List<Long> teachingClassIds = teachingClassList.stream().map(TeachingClass::getTeachingClassId).collect(Collectors.toList());

        List<Long> collect = new ArrayList<>();
        if (CollUtil.isNotEmpty(teachingClassIds)) {
            List<TeachingClassWeek> teachingClassWeekList = teachingClassWeekService.lambdaQuery().in(TeachingClassWeek::getTeachingClassId, teachingClassIds).eq(TeachingClassWeek::getDeleted, Deleted.NOT.getCode()).list();
            collect = teachingClassWeekList.stream().filter(p -> ObjUtil.isNotNull(p.getRoomId())).map(TeachingClassWeek::getRoomId).distinct().collect(Collectors.toList());

        }

        TaskInfo taskInfo = taskInfoService.getById(taskId);
        List<Room> roomList = roomMapper.selectList(new LambdaQueryWrapper<Room>().eq(Room::getSchoolId, taskInfo.getSchoolId()).eq(Room::getTerm, taskInfo.getTerm()).eq(Room::getDeleted, Deleted.NOT.getCode()));
        if (CollUtil.isNotEmpty(collect)) {
            List<Long> finalCollect = collect;
            roomList = roomList.stream().filter(p -> !finalCollect.contains(p.getRoomId())).collect(Collectors.toList());
        }

        return roomList;
    }

    @Override
    public String cleanDistributionsRoom(Integer taskId) {


        ClassInfoByClassResponses classInfoByClass = basicRuleService.getClassInfoByClass(taskId, null, null, 1, 1000);

        List<String> classIds = new ArrayList<>();
        if (ObjUtil.isNotNull(classInfoByClass)) {
            List<ClassInfoVo> facultyClassInfoList = classInfoByClass.getFacultyClassInfoList();
            if (CollUtil.isNotEmpty(facultyClassInfoList)) {
                classIds = facultyClassInfoList.stream().map(ClassInfoVo::getClassId).distinct().collect(Collectors.toList());
            }
        }


        //行政班通过校区进行分组

        if (CollUtil.isNotEmpty(classIds)) {
            classInfoMapper.update(null,new LambdaUpdateWrapper<ClassInfo>()
                    .eq(ClassInfo::getTaskId,taskId).in(ClassInfo::getClassId,classIds)
                    .set(ClassInfo::getRoomId,null));
        }

        return "教室清除完毕";
    }

    @Override
    public String setRoomV2(Integer taskId, String classId, Long roomId, String schoolId) {


        //获取对应行政班然后设置教室，若该教室之前设置过教室，会将其置为空

        TaskInfo taskInfo = taskInfoService.getById(taskId);

        List<TaskInfo> taskInfoList = taskInfoService.lambdaQuery().eq(TaskInfo::getSchoolId, taskInfo.getSchoolId())
                .eq(TaskInfo::getTerm, taskInfo.getTerm()).list();

        List<Integer> taskIds = taskInfoList.stream().map(TaskInfo::getTaskId).distinct().collect(Collectors.toList());

        List<ClassInfo> classInfoList = classInfoService.lambdaQuery().in(ClassInfo::getTaskId, taskIds).list();

        ClassInfo classInfo = classInfoList.stream().filter(p -> ObjUtil.isNotNull(p.getRoomId())).filter(p -> p.getTaskId().equals(taskId) && p.getRoomId().equals(roomId)).findFirst().orElse(null);

        if (ObjUtil.isNotNull(classInfo)) {
            classInfoService.lambdaUpdate().eq(ClassInfo::getId, classInfo.getId()).set(ClassInfo::getRoomId, null).update();
        }

        classInfoService.lambdaUpdate().eq(ClassInfo::getTaskId, taskId).eq(ClassInfo::getClassId, classId).set(ClassInfo::getRoomId, roomId).update();


        return ObjUtil.isNotNull(classInfo) ? "班级" + classInfo.getClassName() + "已修改为空,请及时设置教室，确保流程顺利进行" : "true";
    }
}